package com.hxkcloud.nurse.fragments;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.activitys.DpatientInfoActivity;
import com.hxkcloud.nurse.activitys.SearchPatientActivity;
import com.hxkcloud.nurse.adapter.DisabilityKindAdapter;
import com.hxkcloud.nurse.adapter.DoctorGroupAdapter;
import com.hxkcloud.nurse.adapter.MainPageListAdapter;
import com.hxkcloud.nurse.beans.DisabilityBean;
import com.hxkcloud.nurse.beans.DisabilityListBean;
import com.hxkcloud.nurse.beans.DoctorGroupBean;
import com.hxkcloud.nurse.beans.DoctorPatientBean;
import com.hxkcloud.nurse.beans.DoctorPatientGroupBean;
import com.hxkcloud.nurse.beans.DoctorPatientListBean;
import com.hxkcloud.nurse.beans.DoctorPatientResultBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.config.UrlConfig;
import com.hxkcloud.nurse.dborm.DoctorGroupDao;
import com.hxkcloud.nurse.popup.CalendarClearPopWindow;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.views.CircleImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-10-10.
 * Description:医生端-患者管理
 */
public class DoctorPatientsFragment extends BaseFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, MainPageListAdapter.ResultSelectedListener {

    private View view;

    private LinearLayout ll_title;
    private ImageView iv_search;
    private TextView tv_title;

    private SwipeRefreshLayout swipeRefreshLayout;
    private ExpandableListView expandableListView;
    private MyExpandableListViewAdapter expandAdapter;

    private RecyclerView recyclerView;
    private MainPageListAdapter adapter;
    private List<DoctorPatientBean> patientListDatas;

    private List<DoctorPatientGroupBean> group_list;
    private List<List<DoctorPatientBean>> item_list;


    private TextView countTV;


    private LinearLayout ivgLayout;

    private LinearLayout groupLayout;
    private TextView groupTV;
    private ImageView groupIV;

    private LinearLayout disabilityLayout;
    private TextView disabilityTV;
    private ImageView disabilityIV;

    private LinearLayout timeLayout;
    private TextView timeTV;
    private ImageView timeIV;

    private CalendarClearPopWindow calendarView;

    private PopupWindow allPopup;//选择全部
    private ListView allListView;
    private DoctorGroupAdapter allAdapter;
    private View allWholeView;

    private PopupWindow kindPopup;//选择病种
    private ListView kindListView;
    private DisabilityKindAdapter kindAdapter;

    private View kindWholeView;

    private List<DoctorGroupBean> groupBeans;

    private ObjectResultBean<DisabilityListBean> disabilityResultBean;//残疾程度返回值

    private List<DisabilityBean> disabilityBeans;//残疾程度列表

    private ObjectResultBean<DoctorPatientResultBean> resultBean;//返回结果集

    private List<DoctorPatientGroupBean> patientGroupBeans;//分组列表
    private DoctorPatientListBean patientListBean;//非分组列表

    private String patientCount = "";//非分组人数

    private String groupIdSelected = "";//选中的groupId
    private String disabilitySelected = "";//选中的残疾程度
    private String dateSelected = "";//选中的日期

    private LinearLayout ffzLayout;
    private boolean isFuzhenSelected = false;

    private LinearLayout haveDataLayout;

    private LinearLayout emptyLayout;
    private TextView emptyTV;


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DOCTORPATIENTLIST_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<DoctorPatientResultBean>) msg.obj;
                    if (resultBean.getData().getFzList() != null) {
                        patientGroupBeans = resultBean.getData().getFzList();
                        group_list.clear();
                        item_list.clear();
                        if (patientGroupBeans != null && patientGroupBeans.size() > 0) {
                            expandableListView.setVisibility(View.VISIBLE);
                            for (int i = 0; i < patientGroupBeans.size(); i++) {
                                group_list.add(patientGroupBeans.get(i));
                                item_list.add(patientGroupBeans.get(i).getPatients());
                            }
                            expandableListView.setAdapter(expandAdapter);
                            setListViewHeight(expandableListView);
                        } else {
                            expandableListView.setVisibility(View.GONE);
                        }
                    }
                    if (resultBean.getData().getFfzMap() != null) {
                        if (patientListDatas != null && patientListDatas.size() > 0) {
                            patientListDatas.clear();
                        }
                        patientListBean = resultBean.getData().getFfzMap();
                        patientCount = patientListBean.getPatientCount() + "";
                        countTV.setText(patientCount + getResources().getString(R.string.mainpage_text_person));
                        patientListDatas = patientListBean.getPatients();
                        if (patientListDatas != null && patientListDatas.size() > 0) {
                            recyclerView.setVisibility(View.VISIBLE);
                            ffzLayout.setVisibility(View.VISIBLE);
                            adapter = new MainPageListAdapter(getActivity(), patientListDatas, this, screenWidth);
                            recyclerView.setAdapter(adapter);
//                          adapter.setData(patientListDatas);
                        } else {
                            countTV.setText("0" + getResources().getString(R.string.mainpage_text_person));
                            recyclerView.setVisibility(View.GONE);
                        }

                    }
                    if(isFuzhenSelected){
                        recyclerView.setVisibility(View.GONE);
                        ffzLayout.setVisibility(View.GONE);
                    }else{
                        recyclerView.setVisibility(View.VISIBLE);
                        ffzLayout.setVisibility(View.VISIBLE);
                    }

                }

                break;
            case MessageCode.NETWORKCODE_DOCTORPATIENTLIST_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;


            case MessageCode.NETWORKCODE_DISABILITYLIST_SUCCESS:
                if (msg.obj != null) {
                    disabilityResultBean = (ObjectResultBean<DisabilityListBean>) msg.obj;
                    if (disabilityResultBean.getData().getList() != null && disabilityResultBean.getData().getList().size() > 0) {
                        disabilityBeans.addAll(disabilityResultBean.getData().getList());
                    }
                    kindAdapter.notifyDataSetChanged();
                }

                break;
            case MessageCode.NETWORKCODE_DISABILITYLIST_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mianpage, container, false);
        patientListDatas = new ArrayList<>();
        initView();
        initData();
        initListener();
//        initAllPopup();
        initKindPopup();
        return view;
    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_search = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_search.setVisibility(View.VISIBLE);
        iv_search.setImageResource(R.mipmap.nav_search);
        tv_title.setText(getResources().getText(R.string.usermanger_text));
        iv_search.setOnClickListener(this);

        haveDataLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout_havedata);

        ivgLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout_ivg);

        groupLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout_group);
        groupTV = (TextView) view.findViewById(R.id.mainpage_tv_group);
        groupIV = (ImageView) view.findViewById(R.id.mainpage_iv_group);
        disabilityLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout_disability);
        disabilityTV = (TextView) view.findViewById(R.id.mainpage_tv_disability);
        disabilityIV = (ImageView) view.findViewById(R.id.mainpage_iv_disability);
        timeLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout_time);
        timeTV = (TextView) view.findViewById(R.id.mainpage_tv_time);
        timeIV = (ImageView) view.findViewById(R.id.mainpage_iv_time);

        countTV = (TextView) view.findViewById(R.id.mainpage_tv_count);
        ffzLayout = (LinearLayout) view.findViewById(R.id.mainpage_layout);


        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.mainpage_swiperefresh);
        expandableListView = (ExpandableListView) view.findViewById(R.id.mainpage_expandableListView);
        expandableListView.setDividerHeight(0);


        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressViewOffset(true, -20, 100);

        recyclerView = (RecyclerView) view.findViewById(R.id.mainpage_recyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
//        adapter = new MainPageListAdapter(this, patientListDatas,this, screenWidth);
//        recyclerView.setAdapter(adapter);

        if (calendarView == null) {
            calendarView = new CalendarClearPopWindow(getActivity());
        }
        calendarView.setCalendarListener(new CalendarClearPopWindow.CalendarListener() {
            @Override
            public void setCalendar(String calendar) {
                dateSelected = calendar;
                timeTV.setText(calendar);
                timeTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                timeIV.setImageResource(R.mipmap.tab_arrow);
                ffzLayout.setVisibility(View.GONE);
                isFuzhenSelected = true;

                loading();
                YiJiaHuNetWorkUtils.getDocotrPatientList(groupIdSelected, disabilitySelected, dateSelected, "", handler);

            }
        });

        calendarView.setClearListener(new CalendarClearPopWindow.CalendarClearListener(){

            @Override
            public void clear() {
                dateSelected = "";
                timeTV.setText(mContext.getResources().getString(R.string.fuzhen_time));
                timeTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                timeIV.setImageResource(R.mipmap.tab_arrow);
                isFuzhenSelected = false;
                loading();
                YiJiaHuNetWorkUtils.getDocotrPatientList(groupIdSelected, disabilitySelected, "", "", handler);
            }
        });

        calendarView.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setInitPopup();
            }
        });

        emptyLayout = (LinearLayout) view.findViewById(R.id.ll_department_empty);
        emptyTV = (TextView) view.findViewById(R.id.empty_text);
        emptyTV.setText(getResources().getString(R.string.empty_text_doctorpatients));
        if("".equals(loginInfo.getDepartmentId()) || loginInfo.getDepartmentId() == null){
            emptyLayout.setVisibility(View.VISIBLE);
            haveDataLayout.setVisibility(View.GONE);
        }else{
            emptyLayout.setVisibility(View.GONE);
            haveDataLayout.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initListener() {

        groupLayout.setOnClickListener(this);
        disabilityLayout.setOnClickListener(this);
        timeLayout.setOnClickListener(this);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Intent iP = new Intent(mContext, DpatientInfoActivity.class);
                iP.putExtra("patientid", item_list.get(groupPosition).get(
                        childPosition).getPatientId() + "");
                iP.putExtra("name", item_list.get(groupPosition).get(
                        childPosition).getName());
                if (item_list.get(groupPosition).get(
                        childPosition).getAge() == 0 || "".equals(item_list.get(groupPosition).get(
                        childPosition).getAge() + "")) {
                    iP.putExtra("age", "<1" + getResources().getString(R.string.usermanger_text_age));
                } else {
                    iP.putExtra("age", item_list.get(groupPosition).get(
                            childPosition).getAge() + getResources().getString(R.string.usermanger_text_age));
                }
                if ("0".equals(item_list.get(groupPosition).get(
                        childPosition).getSex())) {
                    iP.putExtra("sex", getResources().getString(R.string.male));
                } else {
                    iP.putExtra("sex", getResources().getString(R.string.female));
                }
                startActivity(iP);
                return true;
            }
        });

    }

    @Override
    protected void initData() {

        //分组数据
        group_list = new ArrayList<DoctorPatientGroupBean>();
        item_list = new ArrayList<List<DoctorPatientBean>>();
        expandAdapter = new MyExpandableListViewAdapter(getActivity());
        loading();
        YiJiaHuNetWorkUtils.getDocotrPatientList("", "", "", "", handler);

    }


    private void setInitPopup() {

        groupTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        groupIV.setImageResource(R.mipmap.tab_arrow);


        disabilityTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        disabilityIV.setImageResource(R.mipmap.tab_arrow);

        timeTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
        timeIV.setImageResource(R.mipmap.tab_arrow);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mainpage_layout_group:
                allPopup.showAsDropDown(ivgLayout);
                setInitPopup();
                groupTV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                groupIV.setImageResource(R.mipmap.tab_arrow_pre);

                break;
            case R.id.mainpage_layout_disability:
                kindPopup.showAsDropDown(ivgLayout);
                setInitPopup();
                disabilityTV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                disabilityIV.setImageResource(R.mipmap.tab_arrow_pre);

                break;
            case R.id.mainpage_layout_time:
                setInitPopup();
                timeTV.setTextColor(mContext.getResources().getColor(R.color.light_blue));
                timeIV.setImageResource(R.mipmap.tab_arrow_pre);
                calendarView.showDropdown(ivgLayout);
                break;
            case R.id.iv_title_back:
                Intent intent = new Intent(getActivity(), SearchPatientActivity.class);
                startActivity(intent);
                break;
        }
    }


    public void initAllPopup() {
        allPopup = new PopupWindow(mContext);
        View allView = LayoutInflater.from(mContext).inflate(R.layout.popup_layout_kind, null);
        allPopup.setContentView(allView);

        allPopup.setWidth(screenWidth);
        allPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        allPopup.setFocusable(true);
        allPopup.setTouchable(true);
        allPopup.setOutsideTouchable(true);
        allPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        DoctorGroupDao dao = new DoctorGroupDao(mContext);
        groupBeans = new ArrayList<>();
        DoctorGroupBean bean = new DoctorGroupBean();
        bean.setIsSelect(false);
        bean.setGroupId("-1");
        bean.setName("全部");
        groupBeans.add(bean);
        groupBeans.addAll(dao.getGroup());
        allListView = (ListView) allView.findViewById(R.id.kind_listview);
        allAdapter = new DoctorGroupAdapter(mContext, groupBeans);
        allListView.setAdapter(allAdapter);
        //初始化组别
        for (int i = 0; i < groupBeans.size(); i++) {
            if (loginInfo.getGroupId().equals(groupBeans.get(i).getGroupId())) {
                groupBeans.get(i).setIsSelect(true);
                groupTV.setText(groupBeans.get(i).getName());
            } else {
                groupBeans.get(i).setIsSelect(false);
            }
        }
        allAdapter.notifyDataSetChanged();

        allListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int i = 0; i < groupBeans.size(); i++) {
                    if (i == position) {
                        groupBeans.get(i).setIsSelect(true);
                    } else {
                        groupBeans.get(i).setIsSelect(false);
                    }
                }

                allAdapter.notifyDataSetChanged();
                groupIdSelected = groupBeans.get(position).getGroupId();
                groupTV.setText(groupBeans.get(position).getName());
                allPopup.dismiss();
                groupTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                groupIV.setImageResource(R.mipmap.tab_arrow);
                loading();
                YiJiaHuNetWorkUtils.getDocotrPatientList(groupIdSelected, disabilitySelected, dateSelected, "", handler);

            }
        });
        allAdapter.setDisplayListener(new DoctorGroupAdapter.DisplayListener() {
            @Override
            public void onDisplay(int position) {

            }
        });

        allWholeView = (View) allView.findViewById(R.id.kind_view_whole);
        allWholeView.setOnClickListener(this);

        allPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setInitPopup();
            }
        });
    }

    private void initKindPopup() {
        kindPopup = new PopupWindow(getActivity());
        View kindView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout_kind, null);
        kindPopup.setContentView(kindView);

        kindPopup.setWidth(screenWidth);
        kindPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        kindPopup.setFocusable(true);
        kindPopup.setTouchable(true);
        kindPopup.setOutsideTouchable(true);
        kindPopup.setBackgroundDrawable(new ColorDrawable(0x00000000));


        disabilityBeans = new ArrayList<>();
        DisabilityBean bean = new DisabilityBean();
        bean.setIsSelect(true);
        bean.setName("全部");
        bean.setDisabilityId("");
        disabilityBeans.add(bean);
        kindListView = (ListView) kindView.findViewById(R.id.kind_listview);
        kindAdapter = new DisabilityKindAdapter(getActivity(), disabilityBeans);
        kindListView.setAdapter(kindAdapter);
        kindListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                for (int i = 0; i < disabilityBeans.size(); i++) {
                    if (i == position) {
                        disabilityBeans.get(i).setIsSelect(true);
                    } else {
                        disabilityBeans.get(i).setIsSelect(false);
                    }
                }

                kindAdapter.notifyDataSetChanged();
                disabilitySelected = disabilityBeans.get(position).getDisabilityId();
                if("".equals(disabilitySelected)){
                    disabilityTV.setText(mContext.getResources().getString(R.string.disability_level));
                }else{
                    disabilityTV.setText(disabilityBeans.get(position).getName());
                }
                kindPopup.dismiss();
                disabilityTV.setTextColor(mContext.getResources().getColor(R.color.black_text));
                disabilityIV.setImageResource(R.mipmap.tab_arrow);
                loading();
                YiJiaHuNetWorkUtils.getDocotrPatientList(groupIdSelected, disabilitySelected, dateSelected, "", handler);
            }
        });
        kindAdapter.setDisplayListener(new DisabilityKindAdapter.DisplayListener() {
            @Override
            public void onDisplay(int position) {

            }
        });

        kindWholeView = (View) kindView.findViewById(R.id.kind_view_whole);
        kindWholeView.setOnClickListener(this);

        kindPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setInitPopup();
            }
        });
        YiJiaHuNetWorkUtils.getDisabilityList(loginInfo.getDepartmentId(), handler);
    }


    @Override
    public void onRefresh() {
        loading();
        YiJiaHuNetWorkUtils.getDocotrPatientList(groupIdSelected, disabilitySelected, dateSelected, "", handler);
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onSelected(String type, int position) {
        if ("item".equals(type)) {
            Intent iP = new Intent(mContext, DpatientInfoActivity.class);
            iP.putExtra("patientid", patientListDatas.get(
                    position).getPatientId() + "");
            iP.putExtra("name", patientListDatas.get(
                    position).getName());

            if(patientListDatas.get(
                    position).getAge() == 0 || "".equals(patientListDatas.get(
                    position).getAge()+"")){
                iP.putExtra("age", "<1" + getResources().getString(R.string.usermanger_text_age));
            }else{
                iP.putExtra("age", patientListDatas.get(
                        position).getAge() + getResources().getString(R.string.usermanger_text_age));;
            }
            if ("0".equals(patientListDatas.get(
                    position).getSex())) {
                iP.putExtra("sex", getResources().getString(R.string.male));
            } else {
                iP.putExtra("sex", getResources().getString(R.string.female));
            }
            startActivity(iP);
        }

    }


    //用过ListView的人一定很熟悉，只不过这里是BaseExpandableListAdapter
    class MyExpandableListViewAdapter extends BaseExpandableListAdapter {

        private Context context;

        public MyExpandableListViewAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getGroupCount() {
            return group_list.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return item_list.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return group_list.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return item_list.get(groupPosition).get(childPosition);
        }

        public void onGroupCollapsed(int groupPosition) {
            setListViewHeight(expandableListView);
        }

        public void onGroupExpanded(int groupPosition) {
            setListViewHeight(expandableListView);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                                 View convertView, ViewGroup parent) {
            GroupHolder groupHolder = null;
            if (convertView == null) {
                convertView = (View) getActivity().getLayoutInflater().inflate(
                        R.layout.expendlist_group, null);
                groupHolder = new GroupHolder();
                groupHolder.groupLayout = (LinearLayout) convertView.findViewById(R.id.expandlist_layout_groupitem);
                groupHolder.week = (TextView) convertView.findViewById(R.id.expandlist_tv_week);
                groupHolder.date = (TextView) convertView.findViewById(R.id.expandlist_tv_date);
                groupHolder.patientCount = (TextView) convertView.findViewById(R.id.expandlist_tv_count);
                groupHolder.img = (ImageView) convertView
                        .findViewById(R.id.img);

                convertView.setTag(groupHolder);
            } else {
                groupHolder = (GroupHolder) convertView.getTag();
            }
            groupHolder.week.setText(group_list.get(groupPosition).getWeek());
            groupHolder.date.setText(group_list.get(groupPosition).getDate());
            groupHolder.patientCount.setText(group_list.get(groupPosition).getPatientCount() + "人");
            groupHolder.img.setBackgroundResource(R.mipmap.mainpage_list_arrow_pre);

            if (isExpanded) {
                groupHolder.img.setBackgroundResource(R.mipmap.mainpage_list_arrow);
                groupHolder.groupLayout.setBackgroundColor(mContext.getResources().getColor(R.color.expandlist_item_color));
            } else {
                groupHolder.img.setBackgroundResource(R.mipmap.mainpage_list_arrow_pre);
                groupHolder.groupLayout.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            }

            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            ItemHolder itemHolder = null;
            if (convertView == null) {
                convertView = (View) getActivity().getLayoutInflater().inflate(
                        R.layout.expendlist_item, null);
                itemHolder = new ItemHolder();
                itemHolder.name = (TextView) convertView.findViewById(R.id.expandlistitem_tv_name);
                itemHolder.age = (TextView) convertView.findViewById(R.id.expandlistitem_tv_age);
                itemHolder.sex = (TextView) convertView.findViewById(R.id.expandlistitem_tv_sex);
                itemHolder.disability = (TextView) convertView.findViewById(R.id.expandlistitem_tv_disability);
                itemHolder.userIcon = (CircleImageView) convertView.findViewById(R.id.expandlistitem_iv_img);
                itemHolder.bottomView = (View) convertView.findViewById(R.id.expandlistitem_bottomView);
                convertView.setTag(itemHolder);
            } else {
                itemHolder = (ItemHolder) convertView.getTag();
            }
            itemHolder.name.setText(item_list.get(groupPosition).get(
                    childPosition).getName());
            if(item_list.get(groupPosition).get(
                    childPosition).getAge() == 0 || "".equals(item_list.get(groupPosition).get(
                    childPosition).getAge()+"")){
                itemHolder.age.setText("<1" + getResources().getString(R.string.usermanger_text_age));
            }else{
                itemHolder.age.setText(item_list.get(groupPosition).get(
                        childPosition).getAge() + getResources().getString(R.string.usermanger_text_age));
            }

            if ("0".equals(item_list.get(groupPosition).get(
                    childPosition).getSex())) {
                itemHolder.sex.setText(getResources().getString(R.string.male));
            } else {
                itemHolder.sex.setText(getResources().getString(R.string.female));
            }

            DisplayImageOptions option = null;
            int age = item_list.get(groupPosition).get(
                    childPosition).getAge();
            if (item_list.get(groupPosition).get(
                    childPosition).getSex().equals("0")) {//male
                if (age > 49) {
                    option = ImageOptionUtils.getInstance().getMan50HeadOptions();
                } else {
                    option = ImageOptionUtils.getInstance().getMan20HeadOptions();
                }
            } else {//female
                if (age > 49) {
                    option = ImageOptionUtils.getInstance().getWoman50HeadOptions();
                } else {
                    option = ImageOptionUtils.getInstance().getWoman20HeadOptions();
                }
            }
            if(item_list.get(groupPosition).get(
                    childPosition).getHeadImage().startsWith("image")){
                imageLoader.displayImage(UrlConfig.image_host + item_list.get(groupPosition).get(
                        childPosition).getHeadImage(), itemHolder.userIcon, option);
            }else{
                imageLoader.displayImage(item_list.get(groupPosition).get(
                        childPosition).getHeadImage(), itemHolder.userIcon, option);
            }

            if (isLastChild) {
                itemHolder.bottomView.setVisibility(View.GONE);
            } else {
                itemHolder.bottomView.setVisibility(View.VISIBLE);
            }


            String selfCare = item_list.get(groupPosition).get(
                    childPosition).getDisabilityName();
            if(TextUtils.isEmpty(selfCare)){
                itemHolder.disability.setVisibility(View.INVISIBLE);
            } else if (selfCare.equals(mContext.getResources().getString(R.string.selfcare_level1))){
                itemHolder.disability.setVisibility(View.VISIBLE);
                itemHolder.disability.setText(mContext.getResources().getString(R.string.degaree_text_level1));
                itemHolder.disability.setBackgroundResource(R.drawable.bg_text_line_level1);
                itemHolder.disability.setTextColor(mContext.getResources().getColor(R.color.color_level1));
            }else if(selfCare.equals(mContext.getResources().getString(R.string.selfcare_level2))) {
                itemHolder.disability.setVisibility(View.VISIBLE);
                itemHolder.disability.setText(mContext.getResources().getString(R.string.degaree_text_level2));
                itemHolder.disability.setBackgroundResource(R.drawable.bg_text_line_level2);
                itemHolder.disability.setTextColor(mContext.getResources().getColor(R.color.color_level2));
            }else if(selfCare.equals(mContext.getResources().getString(R.string.selfcare_level3))) {
                itemHolder.disability.setVisibility(View.VISIBLE);
                itemHolder.disability.setText(mContext.getResources().getString(R.string.degaree_text_level3));
                itemHolder.disability.setBackgroundResource(R.drawable.bg_text_line_level3);
                itemHolder.disability.setTextColor(mContext.getResources().getColor(R.color.color_level3));
            }else if(selfCare.equals(mContext.getResources().getString(R.string.selfcare_level4))) {
                itemHolder.disability.setVisibility(View.VISIBLE);
                itemHolder.disability.setText(mContext.getResources().getString(R.string.degaree_text_level4));
                itemHolder.disability.setBackgroundResource(R.drawable.bg_text_line_level4);
                itemHolder.disability.setTextColor(mContext.getResources().getColor(R.color.color_level4));
            }


            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

    }

    class GroupHolder {
        public LinearLayout groupLayout;
        public TextView week;
        public TextView date;
        public TextView patientCount;
        public ImageView img;
    }

    class ItemHolder {
        public CircleImageView userIcon;
        public TextView name;
        public TextView age;
        public TextView sex;
        public TextView disability;
        public View bottomView;
    }


    private void setListViewHeight(ExpandableListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        int totalHeight = 0;
        int count = listAdapter.getCount();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }


}
